

/**
 * 详情对话框
 */
var InfoDlg = {
    data: {
        isSort: null,
        nodes: [],
    }
};


layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 字典类型表管理初始化
     */
    var DictType = {
        tableId: "dictTypeTable",	//表格id
        condition: {
            searchWords: ""
        }
    };

    /**
     * 初始化表格的列
     */
    DictType.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {title: '类型名称', field: 'name', visible: true, align: 'center', width: '15%', templet: function (d) {
                var url = Lu.ctxPath + '/system/dict?dictTypeId=' + d.id + "&dictTypeName=" + d.name;
                return '<a style="color: #01AAED;" href="' + url + '">' + d.name + '</a>';
            }},
            {title: '类型编码', field: 'code', visible: true, align: 'center', width: '15%', templet: function (d) {
                var url = Lu.ctxPath + '/system/dict?dictTypeId=' + d.id + "&dictTypeName=" + d.name;
                return '<a style="color: #01AAED;" href="' + url + '">' + d.code + '</a>';
            }},
            {title: '状态(字典)', field: 'state',align:'center', width: '7%', templet:function (d) {
                var str = '<input value="'+d.id+'" type="checkbox" name="state" lay-skin="switch" lay-filter="dictTypeSwitch" lay-text="开启|关闭">';
                if(d.state == 1){
                    str = '<input value="'+d.id+'" type="checkbox" name="state" lay-skin="switch" lay-filter="dictTypeSwitch" lay-text="开启|关闭" checked>';
                }
                return str;
            }},
            {title: '修改时间', field: 'updateTime', visible: true, align: 'center', width: '15%'},
            {title: '修改人', field: 'updateUser', visible: true, align: 'center', width: '10%'},
            {title: '字典描述', field: 'description', visible: true, align: 'center', width: '25%'},
            {align: 'center', toolbar: '#task-table-toolbar', title: '操作', width: '10%', fixed:'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    DictType.search = function () {
        var where = {searchWords: $("#searchWords").val()};
        table.reload(DictType.tableId, {where: where});
    };

    /**
     * 字典类型表添加
     */
    DictType.toAdd = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加字典类型表',
            area: ['40%', '60%'],
            content: Lu.ctxPath + '/system/dictType/toAdd',
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
     };

    /**
     * 修改字典类型表
     */
    DictType.toUpdate = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改字典类型表',
            area: ['40%', '60%'],
            content: Lu.ctxPath + '/system/dictType/toUpdate?id=' + data.id,
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
     };

    /**
     * 点击删除
     */
    DictType.onDelete = function (ids) {
        var operation = function () {
            var ajax = new $ax(Lu.ctxPath + "/system/dictType/delete", function (data) {
                Lu.success("删除成功!");
                table.reload(DictType.tableId);
            }, function (data) {
                Lu.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("data", ids);
            ajax.start();
        };
        Lu.confirm("是否刪除?", operation);
    };

    /**
     * 更新缓存
     * @param ids
     */
    DictType.onUpdateCache = function(data){
        var ids = [];
        $.each(data, function (index, d) {
            ids.push(d.id);
        });
        var ajax = new $ax(Lu.ctxPath + "/system/dictType/dictTypeCache", function (data) {
            Lu.success("更新缓存成功!");
        }, function (data) {
            Lu.error("更新缓存失败!" + data.responseJSON.message + "!");
        });
        ajax.set("dictTypeIds", JSON.stringify(ids));
        ajax.start();
    };

    /**
     * 点击排序按钮
     *
     * @param data 点击按钮时候的行数据
     */
    DictType.onSortDictType = function () {
        var isSort = encodeURIComponent('parent.InfoDlg.data.isSort');
        var nodes = encodeURIComponent('parent.InfoDlg.data.nodes');
        var treeUrl = encodeURIComponent('/system/dictType/dictTypeTreeList');
        layer.open({
            type: 2,
            title: '父级菜单',
            area: ['40%', '90%'],
            content: '/system/treeSort?isSort='+isSort+'&nodes='+nodes+'&treeUrl='+treeUrl+'',
            end: function () {
                var isSort = InfoDlg.data.isSort;
                var nodes = InfoDlg.data.nodes;
                if(isSort && nodes && isSort == 1){
                    //排序
                    var ajax = new $ax(Lu.ctxPath + "/system/dictType/sort", function () {
                        Lu.success("排序成功!");
                        InfoDlg.data.isSort = null;
                        InfoDlg.data.nodes = null;
                        table.reload(DictType.tableId);
                    }, function (data) {
                        Lu.error("排序失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("nodes", JSON.stringify(nodes));
                    ajax.start();
                }
            }
        });
    };

   // 渲染表格
   var tableResult = table.render({
        elem: '#' + DictType.tableId,
        url: Lu.ctxPath + '/system/dictType/listPage',
        toolbar: '#dictTypeTableToolbar',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        id: DictType.tableId,
        cols: DictType.initColumn()
   });
   
    var tableReload = () => {
       table.reload(DictType.tableId);
    };

   // 搜索按钮点击事件
   $('#btnSearch').click(function () {
        DictType.search();
   });

   //头工具栏事件
   table.on('toolbar('+DictType.tableId+')', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                //打开添加页面
                DictType.toAdd();
                break;
            case 'deleteBatch':
                var data = checkStatus.data;
                if(data == null || data.length == 0){
                  layer.msg("请至少选择一个!");
                }
                var ids = [];
                $.each(data, function (index, d) {
                    ids.push({id: d.id});
                });
                DictType.onDelete(JSON.stringify(ids));
                break;
            case 'sort':
                DictType.onSortDictType();
                break;
            case 'updateCache':
                DictType.onUpdateCache(checkStatus.data);
                break;
        };
   });

   // 工具条点击事件
   table.on('tool(' + DictType.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'update') {
            DictType.toUpdate(data);
        } else if (layEvent === 'del') {
            DictType.onDelete(JSON.stringify([{id: data.id}]));
        }
    });

    form.on('switch(dictTypeSwitch)', function(data){
        var id = data.value;
        var state = 0;
        if(data.elem.checked){
            state=1;
        }
        var ajax = new $ax(Lu.ctxPath + "/system/dictType/updateState", function (data) {
        }, function (data) {
            Lu.error("操作失败！" + data.responseJSON.message)
        });
        ajax.set({id : id, state : state});
        ajax.start();
    });

});